const req = options => {
  let defaultHeader = {
    "Content-Type": "application/json"
  };
  const token = wx.getStorageSync("token");
  if (token) {
    defaultHeader["token"] = token;
  }

  return new Promise((resolve, reject) => {
    const { data, method } = options;
    wx.request({
      header: Object.assign({}, defaultHeader, options.header),
      url: options.url,
      data,
      method: method || "GET",
      dataType: options.dataType || "json",
      success: result => {
        resolve(result.data);
      },
      fail: reject
    });
  });
};

const login = () => {
  return new Promise((resolve, reject) => {
    wx.login({
      success: resolve,
      fail: reject
    });
  });
};

export default {
  req,
  login
};
